package org.javaforever.gatescore.widgets;

import java.util.ArrayList;
import java.util.List;

import org.javaforever.gatescore.core.FrontDomain;
import org.javaforever.gatescore.core.FrontDropdown;
import org.javaforever.gatescore.core.FrontField;
import org.javaforever.gatescore.core.Statement;
import org.javaforever.gatescore.core.StatementList;
import org.javaforever.gatescore.core.Writeable;
import org.javaforever.gatescore.core.WriteableUtil;
import org.javaforever.gatescore.utils.StringUtil;
import org.javaforever.gatescore.verb.SearchByFieldsByPage;

public class MainDatagrid extends Widget{
	protected FrontDomain domain;

	@Override
	public StatementList generateWidgetStatements() {
		List<Writeable> sList = new ArrayList<>();
		StatementList sl = WriteableUtil.merge(sList);
		sl.setSerial(this.serial);
		return sl;
	}

	@Override
	public StatementList generateWidgetScriptStatements() {
		try {
			List<Writeable> sList = new ArrayList<>();
			long serial = 0L;
			
			sList.add(new Statement(1000L,0,""));
			sList.add(new Statement(2000L,0,"<el-table ref=\"table\" v-loading=\"listLoading\" :data=\"list\" border fit highlight-current-row style=\"width: 100%\" :row-style=\"selectedHighlight\" 　@select-all=\"handleCheckedAll\" 　@selection-change=\"handleChecked\""));
			sList.add(new Statement(3000L,1,":header-cell-style=\"{'background-color': '#3b8fce','color':'white'}\">"));
			sList.add(new Statement(4000L,0,""));
			sList.add(new Statement(5000L,1,"<el-table-column type=\"selection\" align=\"center\" width=\"40px\">"));
			sList.add(new Statement(6000L,1,"</el-table-column>"));
			sList.add(new Statement(7000L,0,""));

			serial = 8000L;
			if (this.domain!=null&&this.domain.hasDomainId()) {
				sList.add(new Statement(serial+1000L,1,"<el-table-column width=\"80px\" align=\"center\" label=\""+this.domain.getDomainId().getText()+"\">"));
				sList.add(new Statement(serial+2000L,1,"<template slot-scope=\"scope\">"));
				sList.add(new Statement(serial+3000L,2,"<span>{{ scope.row."+this.domain.getDomainId().getLowerFirstFieldName()+" }}</span>"));
				sList.add(new Statement(serial+4000L,1,"</template>"));
				sList.add(new Statement(serial+5000L,1,"</el-table-column>"));
			}
			serial += 6000L;
			List<FrontField> fields2 = new ArrayList<FrontField>();
			fields2.addAll(this.domain.getFieldsWithoutId());
			for (FrontField f: fields2){				
				if (f.getFieldType().getTypeName().equalsIgnoreCase("image")) {
					sList.add(new Statement(serial,1,"<el-table-column width=\"160px\" align=\"center\" label=\""+f.getText()+"\" :show-overflow-tooltip=\"true\">"));
					sList.add(new Statement(serial+1000L,2,"<template slot-scope=\"scope\">"));
					sList.add(new Statement(serial+2000L,2,"<img v-if=\"!scope.row.photo\"　:src=\"'../static/images/blank.jpg'\" height=\"50\"/>"));
					sList.add(new Statement(serial+3000L,2,"<img v-else :src=\"'data:image/png;base64,'+scope.row."+f.getLowerFirstFieldName()+"\" height=\"50\"/>"));
				}else {
					if (f instanceof FrontDropdown) {
						sList.add(new Statement(serial,1,"<el-table-column width=\"80px\" align=\"center\" label=\""+f.getText()+"\">"));
						sList.add(new Statement(serial+1000L,1,"<template slot-scope=\"scope\">"));
						sList.add(new Statement(serial+2000L,2,"{{ scope.row."+f.getLowerFirstFieldName()+"  | "+this.domain.getLowerFirstDomainName()+"Filter}}"));
					}else {
						sList.add(new Statement(serial,1,"<el-table-column width=\"80px\" align=\"center\" label=\""+f.getText()+"\">"));
						sList.add(new Statement(serial+1000L,1,"<template slot-scope=\"scope\">"));
						sList.add(new Statement(serial+2000L,2,"<span>{{ scope.row."+f.getLowerFirstFieldName()+" }}</span>"));
					}
				}
				serial += 4000L;
				sList.add(new Statement(serial,1,"</template>"));
				sList.add(new Statement(serial+1000L,1,"</el-table-column>"));
				sList.add(new Statement(serial+2000,0,""));
				serial+=3000L;
			}
			
			sList.add(new Statement(serial+1000L,0,""));
			sList.add(new Statement(serial+2000L,0,"</el-table>"));
			sList.add(new Statement(serial+3000L,0,""));
			SearchByFieldsByPage search = new SearchByFieldsByPage(this.domain);
			sList.add(new Statement(serial+4000L,0,"<pagination v-show=\"total>0\" :total=\"total\" :page.sync=\"listQuery.page\" :limit.sync=\"listQuery.rows\" @pagination=\""+StringUtil.lowerFirst(search.getVerbName())+"\" />"));

			StatementList sl = WriteableUtil.merge(sList);
			sl.setSerial(this.serial);
			return sl;
		}catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	@Override
	public boolean parse() {
		return true;
	}

	public FrontDomain getDomain() {
		return domain;
	}

	public void setDomain(FrontDomain domain) {
		this.domain = domain;
	}

}
